package com.shaoshaohuo.app.ui;

import android.content.Intent;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.shaoshaohuo.app.R;
import com.shaoshaohuo.app.constant.ExtraName;
import com.shaoshaohuo.app.entity.BaseEntity;
import com.shaoshaohuo.app.entity.PointMall;
import com.shaoshaohuo.app.entity.PointMallGoodsDetailEntity;
import com.shaoshaohuo.app.net.HttpRequest.RequestListener;
import com.shaoshaohuo.app.net.RequestService;
import com.shaoshaohuo.app.view.TopbarView;
import com.shaoshaohuo.app.utils.ImageLoadUtil;

public class PointExchangeActivity extends BaseActivity {

	private TopbarView mTopbarView;

	private ImageView mImageView;
	private TextView mTitleText;
	private TextView mPointText;
	private TextView mCountText;
	private TextView mDetailText;
	private Button mExchangeButton;
	private TextView mJiageText;
	
	private String goodsId;

	private PointMall data;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_point_exchange);
		initData();
		initView();
		setUpView();
		requestData();
	}

	private void requestData() {
		startLoadingDialog();
		RequestService.getInstance().getPointGoodsDetail(this, goodsId, PointMallGoodsDetailEntity.class, new RequestListener() {
			
			@Override
			public void onSuccess(int requestCode, BaseEntity resultData) {
				dismissLoadingDialog();
				if(resultData.isOk()){
					PointMallGoodsDetailEntity entity = (PointMallGoodsDetailEntity) resultData;
					updataUi(entity.getData());
				}else{
					showToast(resultData.getMsg());
				}
			}
			
			@Override
			public void onFailed(int requestCode, Exception error, String msg) {
				dismissLoadingDialog();
				showToast(R.string.please_check_network);
			}
		});
	}

	protected void updataUi(PointMall data) {
		this.data = data;
		
//		LoadUtils.getInstance().loadImage(mImageView, Data.getImg());
		 ImageLoader.getInstance().displayImage(data.getImg(), mImageView,ImageLoadUtil.build);
		mTitleText.setText(data.getTitle());
		mPointText.setText(data.getIntegral()+"积分");
		mCountText.setText("份数:"+("-1".equals(data.getFreenum())?"不限数量":data.getFreenum()));
		mDetailText.setText(data.getContent());
		mJiageText.setText("价格:"+data.getPrice());
	}

	private void initData() {
		goodsId = getIntent().getStringExtra(ExtraName.orderid);
	}
//	private ImageView mImageView;
//	private TextView mTitleText;
//	private TextView mPointText;
//	private TextView mCountText;
//	private TextView mDetailText;
//	private Button mExchangeButton;
//	private TextView mJiageText;
	private void initView() {
		mTopbarView = (TopbarView) this.findViewById(R.id.topbar);
		
		mImageView = (ImageView) this.findViewById(R.id.imageview_image);
		mTitleText = (TextView) this.findViewById(R.id.textview_title);
		mPointText = (TextView) this.findViewById(R.id.textview_point);
		mCountText = (TextView) this.findViewById(R.id.textview_count);
		mDetailText = (TextView) this.findViewById(R.id.textvivew_detail);
		mJiageText = (TextView) this.findViewById(R.id.textview_jiage);
		mExchangeButton = (Button) this.findViewById(R.id.button_next);
	}

	private void setUpView() {
		mTopbarView.setCenterText("积分兑换");
		mTopbarView.setLeftView(true, true);

		mJiageText.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
		mExchangeButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				exchange();
			}
		});
	}

	protected void exchange() {
		if(data == null){
			return;
		}
		if(!"-1".equals(data.getFreenum()) && Integer.parseInt(data.getFreenum())<=0){
			showToast("对不起,该商品数量不足");
			return;
		}
		
		Intent intent = new Intent(this,PointExchangeEditActivity.class);
		intent.putExtra(ExtraName.pointGoods, data);
		startActivity(intent);
	}
	
}
